/* Using JRE 1.7.0_07
 * 
 * File: LoginController.java
 * @author Adam Phung
 * Email: adam.phung@gmail.com
 * 
 * Date Created: Oct 3, 2012
 * Date Modified: Oct 30, 2012 
 */

package edu.bu.cs673.superkids123.common.controller;

import java.util.ArrayList;

import javax.swing.JOptionPane;

import edu.bu.cs673.superkids123.AbstractController;
import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.children.model.resources.ResourceType;
import edu.bu.cs673.superkids123.children.view.ActivitiesMenuView;
import edu.bu.cs673.superkids123.common.DataObjectAccess.ChildManager;
import edu.bu.cs673.superkids123.common.controller.exceptions.PlayedTooLongException;
import edu.bu.cs673.superkids123.common.model.Child;
import edu.bu.cs673.superkids123.common.view.ParentsLoginView;

// TODO: Auto-generated Javadoc
/**
 * The Class LoginController.
 */
public class LoginController extends AbstractController{
	
	/**
	 * Instantiates a new login controller.
	 *
	 * @param mainFrame the main frame
	 */
	public LoginController(AbstractFrame mainFrame) {
		super(mainFrame);
	}
	
	/**
	 * List children.
	 *
	 * @return the array list
	 */
	public ArrayList<String> listChildren() {
		ArrayList<String> list = new ArrayList<String>();
		
		for (Child c : ChildManager.getChildren()) {
			list.add(c.getName());
		}
		
		return list;
	}
	
	public ArrayList<Child> getAllChildren() {
		return new ArrayList<Child>(ChildManager.getChildren());
	}

	/**
	 * Login.
	 *
	 * @param name the name
	 */
	public void login(String name) {
		Child child = ChildManager.getChildByName(name);
		try {
			authController.login(child);
			// TODO: Set resource policy
			ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
			if(child.getImageResource()!=null){
				provider.setImagePolicy(child.getImageResource());
			}else{
				provider.setImagePolicy(ResourceType.Default);
			}
			
			if(child.getAudioResource()!=null){
				provider.setAudioPolicy(child.getAudioResource());
			}else{
				provider.setAudioPolicy(ResourceType.Default);
			}
			
			getMainFrame().changeView(ActivitiesMenuView.class);
		} catch (PlayedTooLongException e) {
			JOptionPane.showMessageDialog(null,
					 "You have played too much today! Please talk to you parents about playing more.",
					 "Played too long!", JOptionPane.OK_OPTION);
			return;
		}
		
		
	}
	
	/**
	 * Login parent.
	 */
	public void loginParent() {
		getMainFrame().changeView(ParentsLoginView.class);
	}
}
